package com.java.format;

import org.w3c.dom.css.CSSImportRule;

import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.LinkedList;

public class Formater {

    protected static void numberFormatTest() {
        DecimalFormat df1, df2, df3, df4;

        df1 = new DecimalFormat("#.00");
        df2 = new DecimalFormat("0.00");
        System.out.println(df1.format(0.1));
        System.out.println(df2.format(0.1));

        df1 = new DecimalFormat("0.00");
        df2 = new DecimalFormat("0.##");
        System.out.println(df1.format(0.1));
        System.out.println(df2.format(0.1));

        df1 = new DecimalFormat("0.00");
        df2 = new DecimalFormat("#.00");
        df3 = new DecimalFormat("#.##");
        System.out.println(df1.format(2));
        System.out.println(df2.format(2));
        System.out.println(df3.format(2));

        df4 = new DecimalFormat("#,###.##");
        System.out.println(df4.format(200000));
    }

    protected static void messageFormat() {
        String message = "{0}{1}";
        Object[] array = new Object[]{"A", "B"};
        String output = MessageFormat.format(message, array);
        System.out.println(output);
        System.out.println("=================");

        message = "oh, {0, number, #.##} is a good number";
        array = new Object[]{3.1415926};
        output = MessageFormat.format(message, array);
        System.out.println(output);
        System.out.println("=================");
    }

    protected static void dateFormat() {
        System.out.println("=======================");
        String strDate = "2009-12-24 13:42:15.333";
        String pat1 = "yyyy-mm-dd HH:mm:ss.SSS";
        String pat2 = "yyyy年mm月dd日 HH时mm分ss秒SSS毫秒";
        SimpleDateFormat sdf1 = new SimpleDateFormat(pat1);
        SimpleDateFormat sdf2 = new SimpleDateFormat(pat2);
        Date d = null;
        try {
            d = sdf1.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(sdf2.format(d));
        System.out.println("=======================");
    }

    protected static void dateFormatter() {
        System.out.println("=======================");
        String strDate = "2013年09月12日";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        LocalDate date = LocalDate.parse(strDate, formatter);
        System.out.println(date.getYear() + "-" + date.getMonthValue() + "-" + date.getDayOfMonth());
        System.out.println("=======================");

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss");
        String nowStr = now.format(format);
        System.out.println(nowStr);
    }

    public static void main(String[] args) {
       numberFormatTest();
       messageFormat();
       dateFormat();
       dateFormatter();
    }
}
